package com.aijiang.ui;

import java.awt.AWTException;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.Window.Type;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JRootPane;

public class Index {

	Toolkit toolkit = Toolkit.getDefaultToolkit();

	// 设置窗体默认大小,使其适应屏幕大小
	private int DEFAULE_WIDTH = (int) (toolkit.getScreenSize().getWidth() * 0.1);
	private int DEFAULE_HEIGH = (int) (toolkit.getScreenSize().getHeight() * 0.3);
	// 设置窗体位置,使其在屏幕居中
	private int Location_x = (int) (toolkit.getScreenSize().getWidth() - DEFAULE_WIDTH) - 50;
	private int Location_y = (int) (toolkit.getScreenSize().getHeight() - DEFAULE_HEIGH) - 70;
	static String iconimg = "image/title.png";
	static int mouseAtX = 0;
	static int mouseAtY = 0;

	private JFrame frame;
	public JPanelAnimat panel;
	public TimerControl softtimer;
	public Sleep sleep = new Sleep(10, 0);
	public Wait wait = new Wait(10, 0);
	public Dance dance = new Dance(10, 0);
	public Speak speak = new Speak(10, 0);
	public Jump jump = new Jump(40, 30);
	public Dialog dialog = new Dialog(0, 0);

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {

		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Index window = new Index();
					window.frame.setVisible(true);

					/*
					 * 设置窗体可拖动
					 */
					window.frame.addMouseListener(new MouseAdapter() {
						public void mousePressed(MouseEvent e) {
							/*
							 * 获取点击鼠标时的坐标
							 */
							mouseAtX = e.getPoint().x;
							mouseAtY = e.getPoint().y;
						}
					});

					window.frame.addMouseMotionListener(new MouseMotionAdapter() {
						public void mouseDragged(MouseEvent e) {
							window.frame.setLocation((e.getXOnScreen() - mouseAtX), (e.getYOnScreen() - mouseAtY));// 设置拖拽后，窗口的位置
						}
					});

				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Index() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {

		frame = new JFrame();
		frame.setType(Type.UTILITY);
		frame.setUndecorated(true); // 去掉窗口的装饰
		frame.getRootPane().setWindowDecorationStyle(JRootPane.NONE); // 采用指定的窗口装饰风格
		frame.setBounds(1000, 300, 500, 500);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(DEFAULE_WIDTH, DEFAULE_HEIGH);// 设置窗体默认大小,使其适应屏幕大小
		frame.setLocation(Location_x, Location_y);// 设置窗体在屏幕中的位置

		// 设置窗体透明
		frame.setUndecorated(true);
		frame.setBackground(new Color(0, 0, 0, 0));// alpha分量为零

		// 设置窗体永远在前
		frame.setAlwaysOnTop(true);

		// 设置默认Panel
		panel = new JPanelAnimat(10, 10, 5, this);

		frame.setContentPane(panel);

		softtimer = new TimerControl(this);
		softtimer.start();

		// -------------系统托盘图标设置--------------------------------
		// 判断系统是否支持系统托盘
		if (SystemTray.isSupported()) {
			// 创建系统托盘
			SystemTray tray = SystemTray.getSystemTray();

			// 创建弹出菜单
			PopupMenu popup = new PopupMenu();

			// 主界面选项
			MenuItem mainFrameItem = new MenuItem("OpenAIJiang");
			ActionListener listeneropen = new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					frame.setVisible(true);
				}
			};
			mainFrameItem.addActionListener(listeneropen);

			// 最小化界面
			MenuItem limitFrameItem = new MenuItem("MIniAiJiang");
			ActionListener listenermini = new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					frame.setVisible(false);
				}
			};
			limitFrameItem.addActionListener(listenermini);

			// 退出程序选项
			MenuItem exitItem = new MenuItem("exit");
			exitItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if (JOptionPane.showConfirmDialog(null, "确定退出系统", "AI-Jiang", JOptionPane.YES_NO_OPTION) == 0) {
						System.exit(0);
					}
				}
			});

			popup.add(mainFrameItem);
			popup.add(limitFrameItem);
			popup.add(exitItem);

			// 创建trayIcon
			TrayIcon trayIcon = new TrayIcon(new ImageIcon(iconimg).getImage(), "AI Jiang", popup);
			trayIcon.addActionListener(listeneropen);
			trayIcon.addActionListener(listenermini);
			trayIcon.setImageAutoSize(true);
			try {
				tray.add(trayIcon);
			} catch (AWTException e1) {
				e1.printStackTrace();
			}
		}
		// ---------------------------------------------

	}

}
